package mysql

import (
	"box-fresh-api/boxrpc/boxrpc"

	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	Name  string  `gorm:"type:varchar(50);not null;commit:商品名称'"`
	Price float64 `gorm:"type:decimal(10,2);not null;commit:商品价格'"`
	Num   int64   `gorm:"type:int(11);not null;commit:商品数量'"`
}

func (g *Goods) GoodAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) FindGoodById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(g).Error
}

func GoodList(db *gorm.DB, in *boxrpc.GoodsListRequest) ([]*boxrpc.GoodList, error) {
	var list []*boxrpc.GoodList
	err2 := db.Table("goods").Select("goods.id as goods_id", "name", "price", "num",
		"type_name", "brand_name",
	).
		Joins("LEFT JOIN types ON types.id=goods.id").
		Joins("LEFT JOIN brands ON brands.id=goods.id").
		Debug().Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list).Error
	return list, err2
}
